package com.wm.service.imp;

import java.util.List;

import com.wm.bean.User;
import com.wm.dao.AddressDAO;
import com.wm.dao.UserDAO;
import com.wm.service.UserService;
import com.wm.service.web.PageList;
import com.wm.service.web.Pages;

/**
 * @author yangbo
 *
 * 2013-1-4
 */
public class UserServiceImp implements UserService {

	private UserDAO userDAO;
	private AddressDAO addressDAO;

	public UserDAO getUserDAO() {
		return userDAO;
	}

	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}

	public AddressDAO getAddressDAO() {
		return addressDAO;
	}

	public void setAddressDAO(AddressDAO addressDAO) {
		this.addressDAO = addressDAO;
	}

	
	public User saveUser(User u) {
		return this.getUserDAO().saveUser(u);
	}
	
	public User findUserByUserName(String userName){
		return this.getUserDAO().findUserByUserName(userName);
	}
	
	public User findUserByUserNameUserPwd(String userName,String userPwd){
		return this.getUserDAO().findUserByUserNameUserPwd(userName, userPwd);
	}

	public User updateUser(User u){
		return this.getUserDAO().updateUser(u);
	}

	public User findUserById(long id){
		User u = this.getUserDAO().findUserById(id);
		List addressList = this.getAddressDAO().findAddressByUserId(id);
		u.setAddressList(addressList);
		return u;
	}
	
	public PageList findUsersByUserNameValidIsShow(String userName,int valid,int isShow,String orderby,int ascOrDesc,Pages pages){
		PageList pl = new PageList();
        if (pages.getTotalNum() == -1) {
            pages.setTotalNum(this.getUserDAO().getUserNumByUserNameValidIsShow(userName, valid, isShow));
        }
        pages.executeCount();
        if(pages.getTotalNum() == 0){
        	pl.setObjectList(null);
        	pl.setPages(pages);
        	return pl;
        }
        List l = this.getUserDAO().findUsersByUserNameValidIsShow(userName, valid,isShow,orderby, ascOrDesc, pages.getSpage(), pages.getPerPageNum());
        if(l == null || l.isEmpty()){
	   		 pl.setObjectList(null);
	   	     pl.setPages(pages);
	   		 return pl;
		}else{
			pl.setObjectList(l);
	        pl.setPages(pages);
	        return pl;
		} 
	}
}
